package com.lanxiaoye.shoppingSystem.common;


import com.lanxiaoye.shoppingSystem.common.exception.EmptyException;
import com.lanxiaoye.shoppingSystem.common.exception.BaseException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by libing on 2016/12/13.
 */
public class CheckUtil {
    //判断是否为邮箱格式
    public static boolean checkEmailFormat(String email) throws BaseException, EmptyException {
        try {
            if (email == null) {
                throw new EmptyException("[CheckUtil][checkEmailFormat] Email Empty");
            }
            String RULE_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
            //正则表达式的模式
            Pattern p = Pattern.compile(RULE_EMAIL);
            //正则表达式的匹配器
            Matcher m = p.matcher(email);
            //进行正则匹配
            return m.matches();
        }catch (EmptyException ee){
            throw new EmptyException(ee.getMessage());
        }catch (Exception e){
            throw new BaseException("[CheckUtil][checkEmailFormat] exception");
        }

    }

    //判断是否为手机格式
    public static boolean checkMobileFormat(String phone) throws BaseException, EmptyException {
        try {
            if (phone == null) {
                throw new EmptyException("[CheckUtil][checkMobileFormat] Phone Empty");
            }
            Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
            Matcher m = p.matcher(phone);
            return m.matches();
        }catch (EmptyException ee){
            throw new EmptyException(ee.getMessage());
        }catch (Exception e){
            throw new BaseException("[CheckUtil][checkMobileFormat] exception");
        }
    }

    //判断密码长度是否在6位到15位之间
    public static boolean checkPasswordLength(String password) throws EmptyException, BaseException {
        try {
            if (password == null) {
                throw new EmptyException("[CheckUtil][checkPasswordLength] Password Empty");
            }
            if (password.length() >= 6 && password.length() <= 15)
                return true;
            return false;
        }catch (EmptyException ee){
            throw new EmptyException(ee.getMessage());
        }catch (Exception e){
            throw new BaseException("[CheckUtil][checkPasswordLength] exception");
        }
    }


    //判断密码是否含有字母、数字以及密码长度是否在6-15位之间
    public static boolean checkPasswordFormat(String password) throws EmptyException, BaseException {
        try {
             if (password == null){
                 throw new EmptyException("[CheckUtil][checkPasswordFormat] password Empty");
             }
            final String regx = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$";
        /*
        * 分开来注释一下：
^        匹配一行的开头位置
        (?![0-9]+$) 预测该位置后面不全是数字
        (?![a-zA-Z]+$) 预测该位置后面不全是字母
        [0-9A-Za-z] {8,16} 由8-16位数字或这字母组成
        $ 匹配行结尾位置
        注：(?!xxxx) 是正则表达式的负向零宽断言一种形式，标识预该位置后不是xxxx字符。
        * */
            final Pattern pattern = Pattern.compile(regx);
            final Matcher matcher = pattern.matcher(password);
            return matcher.find();
        }catch (EmptyException ee){
            throw new EmptyException(ee.getMessage());
        } catch (Exception e){
            throw new BaseException("[CheckUtil][checkPasswordFormat] exception");
        }
    }
}
